/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   pdl.h
 * Author: picci
 *
 * Created on May 14, 2011, 7:10 PM
 */

#ifndef PDL_H
#define	PDL_H

#include "prj_definitions.h"

#include <map>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>

namespace NAMESPACE{
const std::string PI_LITERAL    = "([0-9A-Za-z]+)";
const std::string PI_RANGE      = "(\\[([0-9]*:[0-9]+)(:[0-9]+)?\\])";
const std::string PI_PAIR       = "(\\{[0-9A-Za-z]+:[0-9]+\\})";
const std::string PI_PDLELEMENT = PI_LITERAL + "|" + PI_RANGE + "|" + PI_PAIR;
const std::string PI_PDLLIST    = "((" + PI_PDLELEMENT + ")(," + PI_PDLELEMENT + ")*)+";

class PDLElementVisitor;

class PDLElement{
public:
    virtual ~PDLElement(){};
    virtual bool validate(double _value) const = 0;
    virtual bool validate(const std::string& _value) const = 0;
    virtual PDLElement* clone() const = 0;
};

class SerializablePDLElement: public PDLElement{
public:
    virtual ~SerializablePDLElement(){};
    virtual bool validate(double _value) const = 0;
    virtual bool validate(const std::string& _value) const = 0;
    virtual const SerializablePDLElement& serialize(PDLElementVisitor& vis) const = 0;
    virtual PDLElement* clone() const = 0;
    virtual SerializablePDLElement* cloneSerializable() const = 0;
};

// ****** PDLLiteral definition ************************************************

class PDLLiteral : public SerializablePDLElement{
private:
    double value;
    
public:
    ~PDLLiteral();
    PDLLiteral();
    PDLLiteral(double _value);
    PDLLiteral(const PDLLiteral& literal);
    const PDLLiteral& operator=(const PDLLiteral& literal);

    bool validate(double _value) const ;
    bool validate(const std::string& _value) const ;
    const SerializablePDLElement& serialize(PDLElementVisitor& vis) const;
    PDLElement* clone() const;
    SerializablePDLElement* cloneSerializable() const;
    double get_value() const;
};
// -----------------------------------------------------------------------------

// ****** PDLDict definition ***************************************************
class PDLPair : public SerializablePDLElement{
private:
    std::string label;
    int value;

public:
    ~PDLPair();
    PDLPair();
    PDLPair(const std::string& _label, int _value);
    PDLPair(const PDLPair& element);
    const PDLPair& operator=(const PDLPair& element);

    bool validate(double label) const ;
    bool validate(const std::string& label) const ;
    const SerializablePDLElement& serialize(PDLElementVisitor& vis) const;
    PDLElement* clone() const;
    SerializablePDLElement* cloneSerializable() const;
    double get_value() const;
    std::string get_label() const;
};
// -----------------------------------------------------------------------------

// ****** PDLRange definition **************************************************
class PDLRange : public SerializablePDLElement{
private:
    double from, to, scale;
    double step;

public:
    ~PDLRange();
    PDLRange();
    PDLRange(double _from, double _to, double _step = 1);
    PDLRange(const PDLRange& element);
    const PDLRange& operator=(const PDLRange& element);

    bool validate(double _value) const ;
    bool validate(const std::string& label) const ;
    const SerializablePDLElement& serialize(PDLElementVisitor& vis) const;
    PDLElement* clone() const;
    SerializablePDLElement* cloneSerializable() const;
    
    double get_from() const;
    double get_to() const;
    double get_step() const;
    double get_scale() const;
};
// -----------------------------------------------------------------------------

// ****** PDLComposite definition **********************************************
class PDLCompositeIterator;
class PDLCompositeConstIterator;

class PDLComposite : public SerializablePDLElement{
public:
    typedef PDLCompositeIterator iterator;
    typedef PDLCompositeConstIterator const_iterator;
    
    friend class PDLCompositeIterator;
    friend class PDLCompositeConstIterator;
private:
    std::vector< boost::shared_ptr<PDLElement> > elements;

public:
    ~PDLComposite();
    PDLComposite();
    PDLComposite(const PDLComposite& element);
    const PDLComposite& operator=(const PDLComposite& element);

    PDLComposite& add_element(PDLElement& element);

    bool validate(double _value) const ;
    bool validate(const std::string& label) const ;
    const SerializablePDLElement& serialize(PDLElementVisitor& vis) const;
    PDLElement* clone() const;
    SerializablePDLElement* cloneSerializable() const;
    
    iterator begin();
    iterator end();
    const_iterator begin() const;
    const_iterator end() const;
};
// -----------------------------------------------------------------------------

// ****** PDLCompositeIterator *************************************************
class PDLCompositeIterator{
public:
    typedef PDLElement iter_t;
    
private:
    friend class PDLCompositeConstIterator;
    std::vector< boost::shared_ptr<PDLElement> >::iterator iterator;
    std::vector< boost::shared_ptr<PDLElement> >::iterator end_iterator;
    
    PDLComposite* composite;
public:
    
    PDLCompositeIterator(PDLComposite* _composite);
    PDLCompositeIterator(const std::vector< boost::shared_ptr<PDLElement> >::iterator& it);
    
    PDLElement& operator*() const;
    PDLElement* operator->() const;
    
    PDLElement* next();
    SerializablePDLElement* nextSerializable();
    bool has_next() const;

    PDLCompositeIterator& operator++();
    PDLCompositeIterator  operator++(int);

    bool operator==(const PDLCompositeIterator&) const;
    bool operator!=(const PDLCompositeIterator&) const;
    bool operator==(const PDLCompositeConstIterator&) const;
    bool operator!=(const PDLCompositeConstIterator&) const;
};
// -----------------------------------------------------------------------------

// ****** PDLCompositeConstIterator ********************************************
class PDLCompositeConstIterator{
public:
    typedef PDLElement iter_t;
    
private:
    friend class PDLCompositeIterator;
    std::vector< boost::shared_ptr<PDLElement> >::const_iterator iterator;
    
    const PDLComposite* composite;
public:
    
    PDLCompositeConstIterator(const PDLComposite* _composite);
    PDLCompositeConstIterator(const std::vector< boost::shared_ptr<PDLElement> >::const_iterator& it);
    
    const PDLElement& operator*() const;
    const PDLElement* operator->() const;

    PDLCompositeConstIterator& operator++();
    PDLCompositeConstIterator  operator++(int);

    bool operator==(const PDLCompositeConstIterator&)const;
    bool operator!=(const PDLCompositeConstIterator&) const;
    bool operator==(const PDLCompositeIterator&)const;
    bool operator!=(const PDLCompositeIterator&) const;
};
// -----------------------------------------------------------------------------

class PDLParser{
private:
    std::string literal_description;
    std::string range_description;
    std::string pair_description;
    std::string element_description;
    std::string list_description;
public:
    ~PDLParser();
    PDLParser();
    PDLParser(const PDLParser& parser);
    const PDLParser& operator=(const PDLParser& parser);
};
}

#endif	/* PDL_H */

